package com.honyelchak.shortlink.admin.controller;

import com.honyelchak.shortlink.admin.dto.req.UserLoginDTO;
import com.honyelchak.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.honyelchak.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.honyelchak.shortlink.admin.dto.resp.UserActualRespDTO;
import com.honyelchak.shortlink.admin.dto.resp.UserInfoRespDTO;
import com.honyelchak.shortlink.admin.dto.resp.UserRespDTO;
import com.honyelchak.shortlink.admin.service.UserService;
import com.honyelchak.shortlink.base.common.convention.result.Result;
import com.honyelchak.shortlink.base.common.convention.result.Results;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户实体控制层
 * @author Honyelchak
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 通过用户名获取用户，返回脱敏信息
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserByUserName(@PathVariable("username") String username) {
        return Results.success(userService.getUserByUsername(username));
    }

    /**
     * 通过用户名获取用户，返回真实信息
     */
    @GetMapping("/api/short-link/admin/v1/user/actual/{username}")
    public Result<UserActualRespDTO> getUserActualByUserName(@PathVariable("username") String username) {
        return Results.success(userService.getActualUserByUsername(username));
    }

    /**
     * 判断用户名是否存在
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username) {
        return Results.success(userService.hasUsername(username));
    }

    /**
     * 注册用户
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO reqDTO) {
        userService.register(reqDTO);
        return Results.success();
    }

    /**
     * 更新用户
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO reqDTO) {
        userService.update(reqDTO);
        return Results.success();
    }

    /**
     * 使用用户名和token检测是否登录
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> isLogin(@RequestParam("username") String username, @RequestParam("token") String token) {
        return Results.success(userService.isLogin(username, token));
    }

    /**
     * 用户登录
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserInfoRespDTO> login(@RequestBody UserLoginDTO userLoginDTO) {
        return Results.success(userService.login(userLoginDTO));
    }

    /**
     * 用户注销
     */
    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token") String token) {
        userService.logout(username, token);
        return Results.success();
    }

}
